package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private IAppointmentService appointmentService;

    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        Statement statement1 = new Statement();
       statement1.setCustomerName(statement.getCustomerName());
       statement1.setCustomerPhone(statement.getCustomerPhone());
       statement1.setActualArrivalTime(statement.getActualArrivalTime());
       statement1.setCreateTime(new Date());
       statement1.setLicensePlate(statement.getLicensePlate());
       statement1.setCarSeries(statement.getCarSeries());
       statement1.setServiceType(statement.getServiceType());
       statement1.setInfo(statement.getInfo());
        statementMapper.insert(statement1);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {
        Statement statement1 = new Statement();
        //预约中才能修改
        if (!Statement.STATUS_CONSUME.equals(statement1.getStatus())){
            throw new BusinessException("预约中才能修改");
        }
        statement1.setCustomerName(statement.getCustomerName());
        statement1.setCustomerPhone(statement.getCustomerPhone());
        statement1.setActualArrivalTime(statement.getActualArrivalTime());
        statement1.setCreateTime(new Date());
        statement1.setLicensePlate(statement.getLicensePlate());
        statement1.setCarSeries(statement.getCarSeries());
        statement1.setServiceType(statement.getServiceType());
        statement1.setInfo(statement.getInfo());
        statementMapper.updateByPrimaryKey(statement1);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementMapper.deleteByPrimaryKey(dictId);
        }
    }


    @Override
    @Transactional
    public Long generateStatement(Long appointmentId) {
        Statement statement = statementMapper.generateStatement(appointmentId);
        if (statement == null) {
            //该预约单中没有生成结算单，生成结算单修改预约状态
            Appointment appointment = appointmentService.get(appointmentId);
            statement = new Statement();
            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setServiceType(appointment.getServiceType());
            statement.setLicensePlate(appointment.getLicensePlate());
            statement.setInfo(appointment.getInfo());
            statement.setCarSeries(appointment.getCarSeries());
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setAppointmentId(appointmentId);
            //更新预约单状态
            statementMapper.insert(statement);
            appointment.setStatus(Appointment.STATUS_SETTLE);
            appointmentService.updateByStatement(appointment);
        }
        return statement.getId();
    }

    @Override
    @Transactional
    public void pay(Long statementId) {
        //根据传入id查询结算单的信息
        Statement statement = this.get(statementId);
        //处于消费中的记录才能进行支付的操作
        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            throw new BusinessException("请不要重复支付");
        }
        //在结算单中记录谁进行收费与收费的时间
        statement.setPayee(ShiroUtils.getUser());
        statement.setPayTime(new Date());
        //把结算的状态修改成已支付
        statement.setStatus(Statement.STATUS_PAID);
        statementMapper.updateByPrimaryKey(statement);
        //如果该结算单有对应预约单，修改预约单状态改成以上线
        if (statement.getAppointmentId() != null){
            //结算单从预约单中带过来
            appointmentService.cancelOp(statement.getAppointmentId(), Appointment.STATUS_PAID);
        }
    }

    @Override
    public void deleteItemByStatementId(Long statementId) {
        statementMapper.deleteItemByStatementId(statementId);
    }

    @Override
    public void updateByStatementItem(Statement statement) {
        statementMapper.updateByPrimaryKey(statement);
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

}
